//
//  FYStatusOriginView.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/12.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class FYStatusOriginView: UIView {
    
    var originViewConstraint: Constraint?
    // MARK: - 懒加载控件
    //  头像
    private lazy var headImageView: UIImageView = {
        let imageView =  UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 17
        
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    //  认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.orangeColor())
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGrayColor())
    //  微博内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        
        label.numberOfLines = 0
        
        return label
        
    }()
    private lazy var pictureView: FYStatusPictureView = {
        let view = FYStatusPictureView()
        view.backgroundColor = self.backgroundColor

        return view
    }()
    var statusViewModel: FYStatusViewModel? {
        didSet {
            //  设置控件的数据
            if let profile = statusViewModel?.status?.user?.profile_image_url {
                //  绑定头像数据
                headImageView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            contentLabel.text = statusViewModel?.status?.text
            
            sourceLabel.text = statusViewModel?.sourceContent
            
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage 
            
            timeLabel.text = statusViewModel?.timeContent
            
            originViewConstraint?.uninstall()
            
            //  判断原创微博的配图个数是否大于0
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
                
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        //  添加控件
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.originViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    }
}
